package com.huazi.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.huazi.commonutils.CommentResult;
import com.huazi.servicebase.exceptionhandler.MyException;
import com.huazi.vod.service.VodService;

import com.huazi.vod.utils.ConstantVodUtils;
import com.huazi.vod.utils.InitObject;
import org.apache.commons.collections4.Get;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


/**
 * @author shkstart
 * @create 2021-03-22 22:43
 */
@RestController
//@CrossOrigin
@EnableDiscoveryClient
@RequestMapping("/eduvod/video")
public class VodController {

    @Autowired
    private VodService vodService;
    //上传视频到阿里云
    @PostMapping("uploadVideo")
    public CommentResult uploadVideo(MultipartFile file){
        String videoId = vodService.uploadVideo(file);
        return CommentResult.getOK().data("videoId",videoId);
    }
    //删除视频
    @DeleteMapping("deleteVodVideo/{id}")
    public CommentResult deleteVodVideo(@PathVariable("id") String id){
       vodService.deleteVodVideo(id);
       return CommentResult.getOK();
    }
    //根据视频id获取播放凭证
    @GetMapping("getVideoPlayAuth/{id}")
    public CommentResult getVideoPlayAuth(@PathVariable String id){
        try {
            //初始化对象
            DefaultAcsClient client = InitObject.initVodClient(ConstantVodUtils.KEY_ID, ConstantVodUtils.KEY_SECRET);
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();
            request.setVideoId(id);
            response = client.getAcsResponse(request);
            System.out.println(response.getPlayAuth());
            return CommentResult.getOK().data("auth",response.getPlayAuth());
        } catch (ClientException e) {
            e.printStackTrace();
            throw new MyException("获取视频播放凭证失败！");
        }
    }
    //根据视频id获取播放地址
    @GetMapping("getVideoPlaySrc/{id}")
    public CommentResult getVideoPlaySrc(@PathVariable String id){
        try {
            //初始化对象
            DefaultAcsClient client = InitObject.initVodClient(ConstantVodUtils.KEY_ID, ConstantVodUtils.KEY_SECRET);
            GetPlayInfoRequest request = new GetPlayInfoRequest();
            GetPlayInfoResponse response = new GetPlayInfoResponse();
            request.setVideoId(id);
            response = client.getAcsResponse(request);
            List<GetPlayInfoResponse.PlayInfo> infos = response.getPlayInfoList();
            if(infos!=null&&infos.size()>0){

                String playURL = infos.get(0).getPlayURL();

                System.out.println(playURL);

                return CommentResult.getOK().data("src",playURL);
            }
            return CommentResult.getError();
        } catch (ClientException e) {
            e.printStackTrace();
            throw new MyException("获取视频播放凭证失败！");
        }
    }
}
